package cn.chiship.framework.upms.biz.user.service.impl;

import cn.chiship.framework.upms.biz.user.entity.UpmsPostExample;
import cn.chiship.framework.upms.biz.user.mapper.UpmsPostMapper;
import cn.chiship.framework.upms.biz.user.service.UpmsPostService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.upms.biz.user.entity.UpmsPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工作岗位业务接口实现层
 * 2021/11/5
 *
 * @author lijian
 */
@Service
public class UpmsPostServiceImpl extends BaseServiceImpl<UpmsPost, UpmsPostExample> implements UpmsPostService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsPostServiceImpl.class);

    @Resource
    UpmsPostMapper upmsPostMapper;

    @Override
    public BaseResult insertSelective(UpmsPost upmsPost) {
        UpmsPostExample postExample = new UpmsPostExample();
        postExample.createCriteria().andCodeEqualTo(upmsPost.getCode());
        List<UpmsPost> posts = super.selectByExample(postExample);
        if (!posts.isEmpty()) {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "该岗位编号已存在，请重新输入");
        }
        return super.insertSelective(upmsPost);
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(UpmsPost upmsPost) {
        UpmsPostExample postExample = new UpmsPostExample();
        postExample.createCriteria().andCodeEqualTo(upmsPost.getCode()).andIdNotEqualTo(upmsPost.getId());
        List<UpmsPost> posts = super.selectByExample(postExample);
        if (!posts.isEmpty()) {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "该岗位编号已存在，请重新输入");
        }
        return super.updateByPrimaryKeySelective(upmsPost);
    }

}
